﻿using BaseData.API.Write.Applications.MCI.Commands;
using BaseData.Domain.MCI;
using BaseData.ErrorCode;
using BaseData.Infrastructure.Interfaces;
using MediatR;

namespace BaseData.API.Write.Applications.MCI.CommandHandlers
{
    /// <summary>
    /// 删除就诊人信息
    /// </summary>
    public class DelPatientCommandHandler : IRequestHandler<DelPatientCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<Patient> patientRep;

        public DelPatientCommandHandler(IBaseRepository<Patient> patientRep)
        {
            this.patientRep = patientRep;
        }
        public async Task<ApiResult<int>> Handle(DelPatientCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result=new ApiResult<int>();

            var res = await patientRep.GetById(request.PatientId);
            if (res == null)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.删除失败,
                    Message = "该数据不存在!!!",
                    Data = -1
                };
            }
            res.IsDel = true;
            var entity = await patientRep.UpdateAsync(res);
            if (entity > 0)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.删除成功,
                    Message = "删除成功!!!",
                    Data = entity
                };
            }
            else
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.删除失败,
                    Message = "删除失败!!!",
                    Data = entity,
                };
            }

        }
    }
}
